<template>
  <view class="study">
    <text>学习中心{{ text }}</text>
    <view>
      <AtButton
        type="primary"
        open-type="contact"
        :showMessageCard="true"
        send-message-title = "AAAA消息"
        send-message-path="/pages/mine/index?token=1"
        @onContact ="contactCallback"
        send-message-img="https://xthk-online.oss-cn-shenzhen.aliyuncs.com/oss/hp/2019/1573535589_945.png?x-oss-process=image/resize,m_fill,h_402,w_720,limit_0">
        联系客服
      </AtButton>
    </view>
  </view>
</template>

<script>
import { AtButton } from "taro-ui-vue"

export default {
  name: "Index",
  components: {
    AtButton
  },
  data () {
    return {
      text: '11111',
      access_token: '',
      card: true
    }
  },
  onShow() {
    this.getToken()
  },
  methods: {
    getToken() {
      if (this.access_token !== "") {
        return this.access_token;
      }
      // grant_type一定要填client_credential
      let URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc08c69e402e937dc&secret=e302a803a0c0224a75a10558fd58a45b';
      let options = {
        method: "GET",
        url: URL
      };
      return new Promise((resolve, reject) => {
        this.$taro.request(options)
          .then(({ data }) => {
            this.access_token = data.access_token;
            let expires_in = parseInt(data.expires_in) - 10;
            setTimeout(() => {
              this.access_token = '';
              //getAccessToken();
            }, expires_in * 1000);
            resolve(this.access_token);
          })
          .catch((e) => {
            console.log(e)
          })
      });
    },
    contactCallback(e) {
      this.text = '222222222'
      console.log(e)
      /*var path = e.detail.path, query = e.detail.query, params = '';
      if (path) {
        for (var key in query) {
          params = key + '=' + query[key] + '&';
        }
        params = params.slice(0, params.length - 1);
        this.$taro.navigateTo({
          url: path + '?' + params,
          fail: (e) => {
            console.log(e)
          }
        })
      }*/
    }
  }
};
</script>

<style scoped></style>
